﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace kb_24_the_game
{
    class Enemies : ISprite 
    {
        #region variabele declaratie
        public GraphicsDevice graphics { get; set; }

        public SpriteBatch spriteBatch { get; set; }

        public ContentManager content { get; set; }

        public Vector2 position { get; set; }
        int frame = 0;
        int type = 0;
        private float moveSpeed;
        private Animation enemyAnimation = new Animation();
        public Texture2D texture;
        public Vector2 enemyPosition, tempCurrentFrame;
        public string spelerDirectie;
        #endregion variabele declaratie

        public Enemies(ContentManager content, int type, Vector2 position)
        {
            this.content = content;
            this.type = type;
            if (type == 1)
            {
                frame = 4;
                moveSpeed = 4f;
                enemyAnimation.Initialize(position, new Vector2(frame, 1));
            }
            else if (type == 2)
            {
                frame = 4;
                moveSpeed = 3.5f;
                enemyAnimation.Initialize(position, new Vector2(frame, 1));
            }
            else if (type == 3)
            {
                frame = 1;
                moveSpeed = 4f;
                enemyAnimation.Initialize(position, new Vector2(frame, 2));
            }
            tempCurrentFrame = Vector2.Zero;
            Load();
        }

        public void Load()
        {
            if (type == 1)
            {
                texture = content.Load<Texture2D>("Level2Textures/Level2EnemyMeisje");
                enemyAnimation.AnimationImage = texture;
            }
            if (type == 2)
            {
                texture = content.Load<Texture2D>("Level2Textures/Level2EnemyJongen");
                enemyAnimation.AnimationImage = texture;
            }
            if (type == 3)
            {
                texture = content.Load<Texture2D>("Level4Textures/Conducteur");
                enemyAnimation.AnimationImage = texture;
            }
        }
        public void Update(GameTime gameTime)
        {
            enemyAnimation.Active = true;
            enemyPosition = enemyAnimation.Position;
            if (enemyPosition.X + (texture.Width / frame) < -100)
            {
                ResetLinks();
            }
            if (type== 3 && enemyPosition.X + (texture.Width / frame) > 900)
            {
                ResetRechts();
            }
            if (spelerDirectie == "links")
            {
                enemyPosition.X -= moveSpeed - 3;
            }
            else if (spelerDirectie == "rechts")
            {
                enemyPosition.X -= moveSpeed + 3;
            }
            else if (spelerDirectie == "stilstaan")
            {
                enemyPosition.X -= moveSpeed;
            }
            tempCurrentFrame.X = enemyAnimation.CurrentFrame.X;
            enemyAnimation.Position = enemyPosition;
            enemyAnimation.CurrentFrame = tempCurrentFrame;
            enemyAnimation.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            enemyAnimation.Draw(spriteBatch);
        }        
        
        public void SetSpelerDirectie(string directie)
        {
            spelerDirectie = directie;
        }

        public void ResetLinks()
        {
            if (type == 1)
            {
                enemyPosition = new Vector2(800, 449);
                enemyAnimation.Position = new Vector2(800, 449);
            }
            if (type == 2)
            {
                enemyPosition = new Vector2(800, 447);
                enemyAnimation.Position = new Vector2(800, 447);
            }

            if (type == 3)
            {
                tempCurrentFrame.Y = 1;
                moveSpeed *= -1;
                enemyPosition = new Vector2(-25, 403);
                enemyAnimation.Position = new Vector2(-25, 403);
            }
        }
        public void ResetRechts()
        {
            if (type == 3)
            {
                tempCurrentFrame.Y = 0;
                moveSpeed *= -1;
                enemyPosition = new Vector2(800, 403);
                enemyAnimation.Position = new Vector2(800, 403);
            } 
        }

    }
}
